package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.web.dto.ReadInterestChoiceDto;
import com.dingreading.cloud.web.dto.ReadInterestChoiceResultDto;
import com.dingreading.cloud.web.entity.ReadInterestChoice;
import com.dingreading.cloud.web.entity.table.ReadInterestChoiceTableDef;
import com.dingreading.cloud.web.entity.table.ReadInterestQuestionTableDef;
import com.dingreading.cloud.web.entity.table.ReadInterestSuggestTableDef;
import com.dingreading.cloud.web.mapper.ReadInterestChoiceMapper;
import com.dingreading.cloud.web.service.ReadInterestChoiceService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class ReadInterestChoiceServiceImpl extends BaseServiceImpl<ReadInterestChoiceMapper, ReadInterestChoice, ReadInterestChoiceTableDef> implements ReadInterestChoiceService {

    @Override
    protected ReadInterestChoiceTableDef getTable() {
        return ReadInterestChoiceTableDef.readInterestChoice;
    }

    @Override
    public List<ReadInterestChoiceDto> dtoListByQuestionIds(List<Long> questionIds) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.questionId.in(questionIds))
                .orderBy(table.id.asc());
        return getMapper().selectListByQueryAs(wrapper, ReadInterestChoiceDto.class);
    }

    @Override
    public List<ReadInterestChoiceResultDto> listByQuestionInfo(List<Long> choiceIds) {
        ReadInterestQuestionTableDef questionTable = ReadInterestQuestionTableDef.readInterestQuestion;
        ReadInterestSuggestTableDef suggestTable = ReadInterestSuggestTableDef.readInterestSuggest;

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.id, table.questionId, table.rightStatus, table.interestType, table.interestTypeName,
                        questionTable.emphasisType, questionTable.emphasisName, questionTable.emphasisChildType,
                        questionTable.emphasisChildName,
                        suggestTable.content)
                .from(table)
                .leftJoin(questionTable).on(table.questionId.eq(questionTable.id))
                .leftJoin(suggestTable).on(table.emphasisType.eq(suggestTable.emphasisType)
                        .and(table.emphasisChildType.eq(suggestTable.emphasisChildType))
                        .and(table.rightStatus.eq(0)))
                .where(table.id.in(choiceIds))
                .orderBy(questionTable.emphasisType.asc(), questionTable.emphasisChildType.asc());

        return getMapper().selectListByQueryAs(wrapper, ReadInterestChoiceResultDto.class);
    }

}
